<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\Article as mArticle;
use app\admin\controller\Upload;
use think\Db;
use think\Config;

class Article extends Base
{
    protected $beforeActionList = [
        'beforeEdit' => ['only' => 'edit'],
        'columnList' => ['only' => 'edit,add'],
    ];

    //加载所要编辑数据
    protected function beforeEdit()
    {
        //修改前置操作，判断如果
        $request = Request::instance();
        if ($request->isGet() && $request->has('id')) {
            $data = Db::name('article')->find($request->param('id'));
            $this->assign(['data' => $data]);
        } else {
            $this->error('请求参数错误', 'Index/welcome');
        }
    }

    //加载分类
    protected function columnList()
    {
        $data['columnList'] = infinite_classify_1w('column');
//        halt($data);
        return $this->assign($data);

    }

    /*管理页面加载
     * */
    public function index()
    {
        return $this->fetch();
    }

    /*列表数据调用
     * */
    public function lists(Request $request)
    {
        if ($request->isPost()) {
            //判断是否有条件查询
            $where = $request->has('where') ? $request->param()['where'] : [];
            //获取当前页码
            $page = $request->param('page');
            //获取每页显示条数
            $limit = $request->param('limit');
            //过滤空值项
            $where = array_filter($where);

            return mArticle::mAll($page, $limit, $where);
        } else {
            return return_json(1, '请求错误！');
        }
    }

    /*增加(页面加载)
     * */
    public function add()
    {
        return $this->fetch();
    }

    /*
     * 修改(页面加载)
     * */
    public function edit()
    {
        return $this->fetch();
    }

    /*公共：数据增加或者修改
     *
     * */
    public function save(Request $request)
    {
        //判断是否是post请求过来数据
        if ($request->isPost()) {

            //定义默认为增加操作
            $method = ['create', '增加'];
            $data = $request->param()['data'];

            //判断是否是更新数据
            if ($request->has('data.id')) {
                $method = ['update', '更新'];
            }
            //验证输入数据是否合法
            $check = $this->validate($data, $request->controller() . '.'  . $method[0]);
            if (TRUE !== $check) {
                return return_json(1, $check);
            }

            //调用模型增加或者更新静态方法
            return mArticle::mSave($data, $method);
        } else {
            return return_json(1, '请求错误！');
        }
    }

    /*
     * 删除
     * */
    public function del(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->param()['data'];
            return mArticle::mDel($data);
        } else {
            $this->error('请求参数错误', 'index/welcome');
        }
    }

    /*状态修改
     * */
    public function status(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->param()['data'];
            return mArticle::mStatus($data);
        } else {
            $this->error('请求参数错误', 'index/welcome');
        }
    }

    /*上传
     * */
    public function upload(Request $request)
    {
        if ($request->isPost()) {
            $temp_upload_path = ROOT_PATH . 'public' . DS . Config::get('temp_upload_path');

            if ($request->isPost()) {

                //判断是否是替换文件，有就删除
                if ($request->has('old')) {

                    $file_path = ROOT_PATH.'public'.DS . $request->param('old');
                    if (file_exists($file_path)) {
                        @unlink($file_path);
                    }

                }

                $data = [
                    'code' => 200,
                    'url' => '',
                ];

                // 获取表单上传文件
                $files = request()->file('file');
                if ($files) {
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $info = $files->rule('uniqid')->move($temp_upload_path);
                    if ($info) {
                        // 成功上传后 获取上传信息
                        $data['url'] = '/' . Config::get('temp_upload_path') . '/' . $info->getFilename();

                    } else {
                        // 上传失败获取错误信息
                        $data['state'] = $files->getError();
                    }
                } else {
                    $data['code'] = 400;
                }
                return json($data);
            } else {
                return return_json(1, '请求错误！');
            }
        } else {
//            $this->error('请求参数错误','index/welcome');
            return return_json(1, '请求错误！');
        }
    }
}
